#!/bin/sh /etc/rc.common
#
# Copyright (C) 2017 openwrt-cwmpd
#
# This is free software, licensed under the GNU General Public License v3.
# See /LICENSE for more information.
#

START=99
STOP=15

uci_get_by_name() {       
        local ret=$(uci get tozed.$1.$2 2>/dev/null)
        echo ${ret:=$3}                             
}    

uci_get_by_name_yn() {       
        local ret=$(uci get tozed.$1.$2 2>/dev/null)
        local  value=${ret:=$3} 
        if [ $value == y ]; then
           echo 1
        elif [ $value == n ];    then                            
           echo 0
        else
           echo 0
        fi
}       

                                                    
uci_get_by_type() {    
        local ret=$(uci get tozed.@$1[0].$2 2>/dev/null)
        echo ${ret:=$3}                                 
} 
        
gen_service_file() {
        cat <<-EOF >$2
[cwmp]
enable=$(uci_get_by_name_yn $1 tr069_app_enable 0)
interval=$(uci_get_by_name $1 tr069_PeriodicInformInterval 30)
soap_env=SOAP-ENV
soap_enc=SOAP-ENC
acs_auth=$(uci_get_by_name_yn $1 tr069_ACS_auth 0)
cpe_auth=$(uci_get_by_name_yn $1 tr069_CPE_auth 0)
event_filename=/etc/cwmpevent.bin
acs_url=$(uci_get_by_name $1 tr069_ServerURL http://113.98.195.202:7547/)
ca_file=/etc/ssl/private/ca.pem
ca_password=123456
cpe_manufacture=$(uci_get_by_name $1 tr069_Manufacturer 'Shenzhen Tozed Technologies Co., Ltd')
cpe_oui=$(uci_get_by_name $1 tr069_Description D8D866)
cpe_sn=S21$(eth_mac g imei)
cpe_name=$(uci_get_by_name $1 tr069_ModelName 6666)
cpe_pc=$(uci_get_by_name $1 tr069_ProductClass 'ZLT S21')
cpe_specver=$(uci_get_by_name $1 tr069_version V1.0)
cpe_hwver=$(uci_get_by_name $1 tr069_version V1.0)
cpe_version=$(uci_get_by_name $1 tr069_version V1.2.7.29)
cpe_username=$(uci_get_by_name $1 tr069_ConnectionRequestUname cwmp)
cpe_password=$(uci_get_by_name $1 tr069_ConnectionRequestPassword cwmp)
acs_username=$(uci_get_by_name $1 tr069_ServerUsername cwmp)
acs_password=$(uci_get_by_name $1 tr069_ServerPassword cwmp)
cmcc_password=sztozed

[cwmpd]
httpd_ip=
httpd_port=5400
http_timeout=-1
EOF
}

start() {
    local ret=$(uci_get_by_name cfg TZ_TR069_DATA_MODEL device_dialog_lk.xml)
    gen_service_file cfg /etc/cwmp.conf
    cp /etc/$ret /etc/device.xml -a >/dev/null 2>&1
    if [ $(uci_get_by_name_yn cfg tr069_app_enable 1) = 1 ]; then
       /usr/sbin/cwmpd >/dev/null 2>&1 &
    fi
}

stop(){
        service_stop /usr/sbin/cwmpd
}

restart(){
        stop
        start
        
}
